"""
参考文档：
https://flask-admin.readthedocs.io/en/latest/introduction/
https://github.com/flask-admin/flask-admin/tree/master/examples
"""

from wtforms.validators import DataRequired, URL, InputRequired

from commons import CustomModelView
from task_app.tasks import add_apscheduler, edit_apscheduler, remove_apscheduler, modify_apscheduler
from extensions import db


class JobPlanModelView(CustomModelView):
    # can_delete = False
    column_exclude_list = ['method', 'params', 'ext']  # 列表中排除显示这些列
    column_searchable_list = ['task_id']
    column_filters = ['app_name']
    create_modal = True
    form_choices = {
        'trigger_type': [
            ('interval', 'interval'),
            ('date', 'date'),
            ('cron', 'cron')
        ],
        'method': [
            ('GET', 'GET'),
            ('POST', 'POST')
        ],
        'status': [
            ('1', '运行中'),
            ('2', '已暂停'),
            ('3', '已完成')
        ]
    }
    form_args = {
        'task_id': {
            'validators': [InputRequired(), DataRequired()]
        },
        'trigger_type': {
            'validators': [InputRequired(), DataRequired()]
        },
        'url': {
            'validators': [InputRequired(), URL(require_tld=False)]
        },
        'method': {
            'validators': [InputRequired()]
        }
    }

    form_excluded_columns = ['ext', 'next_run_time', 'create_time', 'update_time']  # 表单中排除显示这些列

    column_list = ['app_name', 'task_id', 'name', 'url', 'trigger_type', 'next_run_time',
                   'create_time', 'status_desc']  # 只显示这些列
    column_labels = {
        'app_name': '业务方',
        'task_id': 'TASK_ID',
        'name': '任务名称',
        'method': '请求方法',
        'url': '请求地址',
        'params': '请求参数',
        'trigger_type': '触发',
        'trigger_kwargs': '触发时机',
        'next_run_time': '下次运行时间',
        'status': '任务状态',
        'status_desc': '任务状态',
        'ext': '扩展备注',
        'create_time': '创建时间'
    }

    # Overriding the Built-in Function
    def after_model_change(self, form, model, is_created):
        """ 更新业务定时计划表时，同时操作apscheduler
        apscheduler.add_job()
        apscheduler.pause_job()
        apscheduler.resume_job()
        apscheduler.remove_job()
        """
        if is_created:
            add_apscheduler(model)
        else:
            modify_apscheduler(model)
            edit_apscheduler(model)

    # Overriding the Built-in Function
    def delete_model(self, model):
        """ 删除业务定时计划表时，同时操作apscheduler
        apscheduler.remove()
        """
        db.session.delete(model)
        db.session.commit()
        remove_apscheduler(model)


class JobEvenModelView(CustomModelView):
    can_delete = False
    can_edit = False
    can_create = False
    column_searchable_list = ['task_id']

    column_exclude_list = ['create_time']  # 列表中排除显示这些列
    column_list = ['id', 'task_id', 'method', 'url', 'params', 'result', 'update_time']
    column_labels = {
        'id': 'E_ID',
        'task_id': 'TASK_ID',
        'update_time': '更新时间'
    }
